package com.yx.cloud.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.yx.cloud.Interface.OnItemClickListener;
import com.yx.cloud.R;
import com.yx.cloud.bean.MainMesListBean;
import com.yx.cloud.utils.RomUtils;

import java.util.List;

public class MainMesListAdapte extends RecyclerView.Adapter implements View.OnClickListener {

    private Context mContext;
    private List<MainMesListBean> mMesList;
    private OnItemClickListener mOnItemClickListener = null;

    public MainMesListAdapte(Context context, List<MainMesListBean> list) {
        mContext = context;
        mMesList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mainmeslist_adapte, parent, false);
        ViewHolder holder = new ViewHolder(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            MainMesListBean bean = mMesList.get(position);
            if (bean.getType() == 0) {
                ((ViewHolder) holder).mTvMessageContent.setTextColor(mContext.getResources().getColor(R.color.textview_color2));
                ((ViewHolder) holder).mRlMainMesListAdapter.setBackgroundResource(R.mipmap.main_message1);
                ((ViewHolder) holder).mIvMessageIcon.setImageResource(R.mipmap.main_mes_01_icon);
                ((ViewHolder) holder).mIvMessageClose.setImageResource(R.mipmap.close);
            } else if (bean.getType() == 1) {
                ((ViewHolder) holder).mTvMessageContent.setTextColor(mContext.getResources().getColor(R.color.textview_color2));
                ((ViewHolder) holder).mRlMainMesListAdapter.setBackgroundResource(R.mipmap.main_message1);
                ((ViewHolder) holder).mIvMessageIcon.setImageResource(R.mipmap.mes_accomplish_icon);
                ((ViewHolder) holder).mIvMessageClose.setImageResource(R.mipmap.close);
            } else if (bean.getType() == 2) {
                ((ViewHolder) holder).mTvMessageContent.setTextColor(mContext.getResources().getColor(R.color.white));
                ((ViewHolder) holder).mRlMainMesListAdapter.setBackgroundResource(R.mipmap.main_message2);
                ((ViewHolder) holder).mIvMessageIcon.setImageResource(R.mipmap.clean_error);
                ((ViewHolder) holder).mIvMessageClose.setImageResource(R.mipmap.close2);
            } else if (bean.getType() == 3) {
                ((ViewHolder) holder).mTvMessageContent.setTextColor(mContext.getResources().getColor(R.color.textview_color1));
                ((ViewHolder) holder).mRlMainMesListAdapter.setBackgroundResource(R.mipmap.main_message1);
                ((ViewHolder) holder).mIvMessageIcon.setImageResource(R.mipmap.clean_error2);
                ((ViewHolder) holder).mIvMessageClose.setImageResource(R.mipmap.close);
            }
            ((ViewHolder) holder).mTvMessageContent.setText(bean.getContent());
        }
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return mMesList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout mRlMainMesListAdapter;
        ImageView mIvMessageIcon;
        TextView mTvMessageContent;
        ImageView mIvMessageClose;

        public ViewHolder(View itemView) {
            super(itemView);
            mIvMessageIcon = itemView.findViewById(R.id.tv_messageIcon);
            mTvMessageContent = itemView.findViewById(R.id.tv_messageContent);
            mIvMessageClose = itemView.findViewById(R.id.tv_messageClose);
            mRlMainMesListAdapter = itemView.findViewById(R.id.rl_mainMesListAdapter);
        }
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v, (Integer) v.getTag());
        }
    }

    /**
     * Item点击事件方法
     *
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

}
